package com.ihisomp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ihisomp.entity.OrgUserRole;
import com.ihisomp.mapper.OrgUserRoleMapper;
import com.ihisomp.service.IOrgUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户与角色关系 服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-04-18
 */
@Service
public class OrgUserRoleServiceImpl extends ServiceImpl<OrgUserRoleMapper, OrgUserRole> implements IOrgUserRoleService {

    /**
     * 根据用户编码删除用户对应角色
     * @param userKey
     * @return
     */
    @Override
    public int deleteByUserKey(String userKey) {
        return baseMapper.deleteByUserKey(userKey);
    }

    /**
     * 根据用户编码查询用户对应角色
     * @param userKey
     * @return
     */
    @Override
    public List<String> getRoleByUserKey(String userKey) {
        QueryWrapper<OrgUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_key", userKey);
        queryWrapper.orderByAsc("role_key");
        List<OrgUserRole> orgUserRoles = baseMapper.selectList(queryWrapper);
        List<String> list = new ArrayList();
        for (int i = 0; i < orgUserRoles.size(); i++) {
            list.add(orgUserRoles.get(i).getRoleKey());
        }
        return list;
    }
}
